/*
* Equipment.java
*
* Created on 16-Dec-2007, 23:56:41
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.killerscape.model.player;

import net.killerscape.model.Player;
import net.killerscape.test.Main;
import net.killerscape.util.Logger;

public class Equipment {
    private Player player;
    private EquippedItem[] items = new EquippedItem[14];
    private static final int FRAME = 1688;
    
	public Equipment(Player p) {
		this.player = p;
	}

	public int getId(int type) {
		if(!isEquipped(type))
			return -1;
		return items[type].getItemId();
	}

	public int getAmount(int type) {
		if(!isEquipped(type))
			return -1;
			return items[type].getAmount();
	}

	public void equipItem(int type, int id, int slot) {
		int amount = player.getInventory().getAmount(slot);
		int oldItem = -1;
		int oldAmount = -1;
		boolean flag = false;
		if(items[type] != null) {
			flag = true;
			oldAmount = items[type].getAmount();
			oldItem = items[type].getItemId();
			items[type].setItemId(id);
			items[type].setAmount(amount);
		} else
			items[type] = new EquippedItem(id, amount);
		if(type == WEAPON) {
			int[] animations = Main.weaponHandler.getAnimations(id);
			player.setWeaponEmotes(animations[0],animations[1],animations[2], animations[0], animations[0], animations[0], animations[0]);
		}
		player.getInventory().removeItemAtSlot(slot);
		if(flag)
			player.getInventory().addItem(oldItem,oldAmount);
		player.getActionSender().sendItem(id, amount, WEAPON, FRAME);
		player.appearanceUpdateRequired();
	}
	
	public void updateEquipScreen() {
		for(int i = 0; i < 14; i++)
			player.getActionSender().sendItem(items[i].getItemId(), items[i].getAmount(), i, FRAME);
	}
	
	public void updateEquipScreenItem(int type) {
		if(items[type] != null)
			player.getActionSender().sendItem(items[type].getItemId(), items[type].getAmount(), type, FRAME);
		else
			player.getActionSender().sendItem(-1, -1, type, FRAME);
	}
	
	public void unequipItem(int type) {
		if(!isEquipped(type)) {
			Logger.err("Error - item doesn't exist - possible cheating");
			return;
		} else {
			player.getInventory().addItem(items[type].getItemId(), items[type].getAmount());
			items[type] = null;
			updateEquipScreenItem(type);
			player.appearanceUpdateRequired();
		}
	}
	
	public boolean isEquipped(int type) {
		return items[type] != null;
	}

static class EquippedItem {
    private int itemId, amount;

	public EquippedItem(int id, int a) {
		this.itemId = id;
		this.amount = a;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
}

public static final int HAT=0, CAPE=1, AMULET=2, WEAPON=3,
			TORSO=4, SHIELD=5, LEGS=7, HANDS=9, FEET=10,
			RING=12, ARROWS=13;
}